<?php
// 本类由系统自动生成，仅供测试用途
class DetailAction extends Action {
	public function index(){
		$detail = M('detail');
		$where['id'] = I('id');
		$one = $detail->where($where)->find();
		//颜色
		if(strpos($one['color'],',')){
			$one['color'] = explode(',',$one['color']);
		}else{
			$one['color'] = (array)$one['color'];	
		}
		
		//颜色图片
		if(strpos($one['color_img'],',')){
			$one['color_img'] = explode(',',$one['color_img']);
		}else{
			$one['color_img']=(array)$one['color_img'];	
		}
		
		foreach($one['color'] as $key=>$value){
			$one['colorNew'][$key]=array($one['color'][$key],$one['color_img'][$key]);
		}
		
		//尺码
		if(strpos($one['size'],';')){
			$one['size'] = explode(';', $one['size'])[0];
			$one['size'] = explode(',', $one['size']);
		}else{
			if(strpos($one['size'],',')){
				$one['size'] = explode(',', $one['size']);
			}else{
				$one['size'] =(array)$one['size'];
			}	
		}
		
		//展示小图
		if(strpos($one['small_img'],';')){
			$one['small_img'] = explode(';',$one['small_img']);
			foreach($one['small_img'] as $key=>$value){
				if(strpos($one['small_img'][$key],',')){
					$one['small_img_new'][$key] = explode(',',$one['small_img'][$key]);
				}else{
					$one['small_img_new'][$key] =(array)$one['small_img'][$key];
				}
			}
		}else{
			if(strpos($one['small_img'],',')){
				$one['small_img_new'] = explode(',',$one['small_img']);
			}else{
				$one['small_img_new'] =(array)$one['small_img'];
			}
		}
		if(is_array($one['small_img_new'][0])){
			foreach($one['small_img_new'] as $key=>$value){
				foreach($value as $k=>$v){
					$arr_small_img[]=$v;
				}
			}
			$one['small_img_new'] = $arr_small_img;
		}
				
		//展示图
		if(strpos($one['show_img'],',')){
			$one['show_img'] = explode(',',$one['show_img']);
		}else{
			$one['show_img'] =(array)$one['show_img'];
		}
		//商品属性
		$one['attribute'] =explode(",", $one['attribute']);
		foreach($one['attribute'] as $key => $value){
			$one['attributeNew'][$key] = explode(':', $one['attribute'][$key]);
		}
		
		//折扣
		$one['discount'] = substr($one['n_price']/$one['b_price'], 0,3);

        //展示评论内容
		//获取session
		$uid = session('uid');
		//评论内容表
		$comment = M('comment');
		//订单详情表
        $orderList = M('order_list');
        //用户信息表
        $users = M('users');
        //查询评论
        $whereComment['goodsId'] = I('id');
		$commentList = $comment->where($whereComment)->select();
        $count = count($commentList);
        $this->assign('count',$count);
        foreach ($commentList as $key=>$value){
            //查询订单内容
            $whereOrders['userId'] = $commentList[$key].userId;
            $whereOrders['goodsId'] = I('id');
            $orderInfo = $orderList->where($whereOrders)->find();
            //用户信息查询
            $whereUser['id'] = $commentList[$key].userId;
            $userInfo = $users->where($whereUser)->find();
            //往评论中填充信息
            $commentList[$key]['color'] = $orderInfo.color;
            $commentList[$key]['size'] = $orderInfo.size;
            $commentList[$key]['address'] = $userInfo.address;
            $commentList[$key]['email']=$userInfo.email;
            $commentList[$key]['addTime'] = date("Y-m-d h:i:sa", $commentList[$key]['addTime']);
        }
//        echo "<pre>";
//        var_dump($one);
		$this->assign('comment',$commentList);
		$this->assign('uid',$uid);
		$this->assign('content',$one);

		//列表
        $list = $detail->select();
        foreach ($list as $key => $value) {
            if (strpos($list[$key]['color_img'], ';')) {
                $list[$key]['color_img'] = explode(';', $list[$key]['color_img'])[0];
                if (strpos($list[$key]['color_img'], ',')) {
                    $list[$key]['color_img'] = (String)explode(',', $list[$key]['color_img'])[0];
                }
            } else {
                if (strpos($list[$key]['color_img'], ',')) {
                    $list[$key]['color_img'] = (String)explode(',', $list[$key]['color_img'])[0];
                }
            }
        }
        $this->assign('rightList',$list);
        shuffle($list);
        $this->assign('leftList',$list);







		$this->display('detail');
	}

	//添加购物车
	public function addCar(){
		//实例化购物车表
		$shoppingCar = M('shopping_car');
		$uid = $_SESSION['uid'];
		$dataCar['userId'] = $uid;
		$dataCar['goodsId'] = I('goodsId');
		$dataCar['color'] = I('color');
		$dataCar['size'] = I('size');
		$dataCar['goodsNum'] = I('goodsNum');
		$where['userId'] = $uid;
		$where['goodsId']=I('goodsId');
		$where['color']=I('color');
		$where['size']=I('size');
		$oneMore = $shoppingCar->where($where)->find();
		if($oneMore){
			$dataAdd['goodsNum'] = $oneMore['goodsNum']+I('goodsNum');
			$whereAdd['userId'] = $uid;
			$whereAdd['goodsId'] = I('goodsId');
			$res=$shoppingCar->where($whereAdd)->save($dataAdd);
			if($res){
				if($res){
					$this->ajaxReturn('','',1);
				}else{
					$this->ajaxReturn('','',0);
				}	
			}
		}else{
			$res = $shoppingCar->add($dataCar);
			if($res){
				$this->ajaxReturn('','',1);
			}else{
				$this->ajaxReturn('','',0);
			}
		}	
	}
}